package practice.unionfind;

import java.util.Arrays;

/**
 * @Description:
 * @Author Ammar
 * @Create 2023/8/26 19:36
 */
public class QuickUnion_size extends QuickUnion {
    int[] sizes;
    public QuickUnion_size(int capacity) {
        super(capacity);
        sizes = new int[parent.length];
        Arrays.fill(sizes, 1);
    }
    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        if (sizes[p1] > sizes[p2]) {
            parent[p2] = p1;
        } else if (sizes[p1] < sizes[p2]) {
            parent[p1] = p2;
        } else {
            parent[p2] = p1;
            sizes[p1] = sizes[p1] + sizes[p2];
        }
    }
}
